package com.asset.dispoal.business.approval.service.command;

import com.asset.dispoal.business.approval.domain.db.mapper.RepaymentApproverMapper;
import com.asset.dispoal.business.approval.domain.db.qo.GetRepaymentApprovalListQo;
import com.asset.dispoal.business.approval.dto.GetRepaymentApprovalListRequest;
import com.asset.dispoal.business.approval.dto.RepaymentApprovalResponse;
import com.asset.dispoal.business.approval.service.converter.ApprovalConverter;
import com.asset.dispoal.commom.menus.AgreementCaseSateMenu;
import com.asset.dispoal.commom.page.PagingQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import tech.mhuang.pacebox.core.ddd.BaseCommand;
import tech.mhuang.pacebox.springboot.core.local.GlobalHeaderThreadLocal;
import tech.mhuang.pacebox.springboot.protocol.Result;
import tech.mhuang.pacebox.springboot.protocol.data.Page;
import tech.mhuang.pacebox.springboot.protocol.data.PageVO;

import java.util.List;

/**
 * @description:
 * @author: yf
 * @date: 2022-10-09 15:54:46
 */
@Repository
public class GetApprovalListCommand implements BaseCommand<GetRepaymentApprovalListRequest, Result<PageVO<RepaymentApprovalResponse>>> {
    @Autowired
    private RepaymentApproverMapper repaymentApproverMapper;

    @Override
    public Result<PageVO<RepaymentApprovalResponse>> execute(GetRepaymentApprovalListRequest request) {
        GetRepaymentApprovalListQo approvalListQo = ApprovalConverter.INSTANCE.reqToQo(request);
        String userId = GlobalHeaderThreadLocal.getOrException().getUserId();

        approvalListQo.setCurrentUserId(userId);
        int count = repaymentApproverMapper.countByQo(approvalListQo);
        Page page = PagingQuery.getPage(count, approvalListQo);
        PageVO<RepaymentApprovalResponse> pageVO = new PageVO<>();
        pageVO.setTotalSize(count);

        if (page != null) {
            List<RepaymentApprovalResponse> responseList = repaymentApproverMapper.queryApprovalList(page);
            responseList.forEach(response -> response.setRepaymentCaseState(AgreementCaseSateMenu.getByLabel(response.getRepaymentCaseState()).getValue()));
            pageVO.setResult(responseList);
        }
        return Result.ok(pageVO);
    }
}
